//
//  CreateJobViewController.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/30.
//

import UIKit
import RxCocoa
import RxSwift
class CreateJobViewController: BaseViewController,UIGestureRecognizerDelegate {
    lazy var disposeBag = DisposeBag()
    let selectJobTypeVC = CreateJobSelectJobTypeVC()
    lazy var companyAgentVC = CreateJobCompanyAgentVC()
    lazy var  installAddressVC = CreateJobInstallAddressVC()
    lazy var  siteAttributesVC = CreateJobSiteAttributesVC()
    lazy var  ownerInfomationVC = CreateJobOwnerInfomationVC()
    lazy var  scheculeDateOfInstallVC = CreateJobScheculeDateOfInstallVC()
    lazy var  systemDetailVC = CreateJobSystemDetailVC()
    lazy var  installerDetailVC = CreateJobInstallerDetailVC()
    lazy var  reviewDetailVC = CreateJobReviewDetailVC()
    lazy var   createSuccessVC = CreateJobSuccessVC()
    override func viewDidLoad() {
        super.viewDidLoad()
        //初始化View Model
        CreateJobModelHelper.shared.createJobViewModel = CreateJobViewModel()
        //1.第一个界面
        self.addChildVC(selectJobTypeVC)
        self.dealSelectJobTypeVCOperBtns()
        
        //开启右滑返回功能
        self.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
        //右滑返回代理
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
        
    }
    //MARK: - UIGestureRecognizerDelegate ,自己处理右滑返回代理
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        Tool().showAlertViewWithTwoBtns(title: "Cancel Form Creation",message: "Are you sure you want to cancel?", yesClosure:  {
            self.navigationController?.popToRootViewController(animated: true)
        })
        return false
    }
    
    private func toNextVC(fromVC: UIViewController, toVC: UIViewController){
        if self.checkIfChildVCExist(toVC) {
            self.transition(from: fromVC, to: toVC, duration: 0.5,animations: nil)
        }else{
            self.addChildVC(toVC)
            //add 相应的vc后，才执行相关的处理代码；
            if toVC.isKind(of: CreateJobSelectJobTypeVC.self){
                self.dealSelectJobTypeVCOperBtns()
            }else if toVC.isKind(of: CreateJobCompanyAgentVC.self){
                self.dealCompanyAgentVCOperBtns()
            }else if toVC.isKind(of: CreateJobInstallAddressVC.self){
                self.dealInstallAddressVCOperBtns()
            }else if toVC.isKind(of: CreateJobSiteAttributesVC.self){
                self.dealSiteAttributesVCOperBtns()
            }else if toVC.isKind(of: CreateJobSiteAttributesVC.self){
                self.dealSiteAttributesVCOperBtns()
            }else if toVC.isKind(of: CreateJobOwnerInfomationVC.self){
                self.dealOwnerInfomationVCOperBtns()
            }else if toVC.isKind(of: CreateJobScheculeDateOfInstallVC.self){
                self.dealScheculeDateOfInstallVCOperBtns()
            }else if toVC.isKind(of: CreateJobSystemDetailVC.self){
                self.dealSystemDetailVCOperBtns()
            }else if toVC.isKind(of: CreateJobInstallerDetailVC.self){
                self.dealInstallerDetailVCOperBtns()
            }else if toVC.isKind(of: CreateJobReviewDetailVC.self){
                self.dealReviewDetailVCOperBtns()
            }
        }
    }
    //MARK: - *****************各个VC上continue， back to pre的处理
    //MARK: - 处理select job type上按钮
    private func dealSelectJobTypeVCOperBtns(){
        //2.从1跳转到2
        self.selectJobTypeVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.selectJobTypeContinue()
        }
        //*****点击左上角的返回按钮
        self.selectJobTypeVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            //从select type返回，要提示是否取消操作，因为可能用户来回返回编辑，这样提示，防止用户丢失创建数据
            strongSelf.selectJobTypeVC.customView.cancelBtnClick()
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理company agent上按钮
    private func dealCompanyAgentVCOperBtns(){
        //2.从1跳转到2
        self.companyAgentVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.companyAgentContinue()
        }
        //*****点击左上角的返回按钮
        self.companyAgentVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.companyAgentVC.editFlag {
                strongSelf.companyAgentVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.companyAgentVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.companyAgentVC, toVC: strongSelf.selectJobTypeVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理InstallAddress上的continue, back to pre 按钮的处理
    private func dealInstallAddressVCOperBtns(){
        //3.从2跳转到3
        self.installAddressVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.installAddressContinue()
        }
        //*****点击左上角的返回按钮
        self.installAddressVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.installAddressVC.editFlag {
                strongSelf.installAddressVC.getData()
                strongSelf.installAddressVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.installAddressVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.installAddressVC, toVC: strongSelf.companyAgentVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理SiteAttributes上的continue, back to pre 按钮的处理
    private func dealSiteAttributesVCOperBtns(){
        //4.从3跳转到4
        self.siteAttributesVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.siteAttributesContinue()
        }
        //*****点击左上角的返回按钮
        self.siteAttributesVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.siteAttributesVC.editFlag {
                strongSelf.siteAttributesVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.siteAttributesVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.siteAttributesVC, toVC: strongSelf.installAddressVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理ownerInfomation上的continue, back to pre 按钮的处理
    private func dealOwnerInfomationVCOperBtns(){
        //5.从4跳转到5
        self.ownerInfomationVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.ownerInfomationContinue()
        }
        //*****点击左上角的返回按钮
        self.ownerInfomationVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            CreateJobModelHelper.shared.createJobViewModel.propertyOwner = strongSelf.ownerInfomationVC.customView.viewModel
            if strongSelf.ownerInfomationVC.editFlag {
                strongSelf.ownerInfomationVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.ownerInfomationVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.ownerInfomationVC, toVC: strongSelf.siteAttributesVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理ScheculeDateOfInstall上的continue, back to pre 按钮的处理
    private func dealScheculeDateOfInstallVCOperBtns(){
        //6.从5跳转到6
        self.scheculeDateOfInstallVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.scheculeDateOfInstallContinue()
        }
        //*****点击左上角的返回按钮
        self.scheculeDateOfInstallVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.scheculeDateOfInstallVC.editFlag {
                strongSelf.scheculeDateOfInstallVC.getData()
                strongSelf.scheculeDateOfInstallVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.scheculeDateOfInstallVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.scheculeDateOfInstallVC, toVC: strongSelf.ownerInfomationVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理ScheculeDateOfInstall上的continue, back to pre 按钮的处理
    private func dealSystemDetailVCOperBtns(){
        //7.从6跳转到7
        self.systemDetailVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.systemDetailContinue()
        }
        //*****点击左上角的返回按钮
        self.systemDetailVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.systemDetailVC.editFlag {
                strongSelf.systemDetailVC.getData()
                strongSelf.systemDetailVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.systemDetailVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.systemDetailVC, toVC: strongSelf.scheculeDateOfInstallVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理ScheculeDateOfInstall上的continue, back to pre 按钮的处理
    private func dealInstallerDetailVCOperBtns(){
        //8.从7跳转到8
        self.installerDetailVC.customView.continueClosure  = {[weak self]  in
            guard let strongSelf = self else {return}
            strongSelf.installerDetailContinue()
        }
        //*****点击左上角的返回按钮
        self.installerDetailVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.installerDetailVC.editFlag {
                strongSelf.installerDetailVC.editFlag = false
                strongSelf.toNextVC(fromVC: strongSelf.installerDetailVC, toVC: strongSelf.reviewDetailVC)
            }else{
                strongSelf.toNextVC(fromVC: strongSelf.installerDetailVC, toVC: strongSelf.systemDetailVC)
            }
        }.disposed(by: self.disposeBag)
    }
    //MARK: - 处理Review上按钮的处理
    private func dealReviewDetailVCOperBtns(){
        //*****点击左上角的返回按钮
        self.reviewDetailVC.customView.backToPreBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.installerDetailVC)
        }.disposed(by: self.disposeBag)
        //******从Review到各个地方再编辑
        //Edit Summary
        self.reviewDetailVC.customView.summaryView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.companyAgentVC.editFlag = true
            strongSelf.companyAgentVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.companyAgentVC)
        }
        //Edit InstallAddress
        self.reviewDetailVC.customView.installAddressView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.installAddressVC.editFlag = true
            strongSelf.installAddressVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.installAddressVC)
        }
        //Edit Site attributes
        self.reviewDetailVC.customView.siteAttributeView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.siteAttributesVC.editFlag = true
            strongSelf.siteAttributesVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.siteAttributesVC)
        }
        //Edit ownerInfo
        self.reviewDetailVC.customView.ownerInfoView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.ownerInfomationVC.editFlag = true
            strongSelf.ownerInfomationVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.ownerInfomationVC)
        }
        //Edit schedule data of install
        self.reviewDetailVC.customView.installDateView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.scheculeDateOfInstallVC.editFlag = true
            strongSelf.scheculeDateOfInstallVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.scheculeDateOfInstallVC)
        }
        //Edit system detail
        self.reviewDetailVC.customView.systemDetailView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.systemDetailVC.editFlag = true
            strongSelf.systemDetailVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.systemDetailVC)
        }
        //Edit technicianDetails
        self.reviewDetailVC.customView.technicianDetailsView.titleView.editClosure = {[weak self] in
            guard let strongSelf = self else {return}
            strongSelf.installerDetailVC.editFlag = true
            strongSelf.installerDetailVC.customView.createJobReviewEditFlag = true
            strongSelf.toNextVC(fromVC: strongSelf.reviewDetailVC, toVC: strongSelf.installerDetailVC)
        }
    }
    //MARK: - *****************各个continue的具体处理
    //MARK: - 点击continue
    private func selectJobTypeContinue() {
        self.toNextVC(fromVC: selectJobTypeVC, toVC: companyAgentVC)
    }
    //MARK: - 点击continue
    private func companyAgentContinue() {
        //在Review中点击Edit编辑时，才校验
        if self.companyAgentVC.customView.createJobReviewEditFlag {
            CreateJobModelHelper.shared.checkIfInstallDetailValid(loadingFlag: false)
        }
        if self.companyAgentVC.editFlag == true {//编辑
            self.companyAgentVC.editFlag = false
            self.companyAgentVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.companyAgentVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: companyAgentVC, toVC: installAddressVC)
        }
    }
    //MARK: -  点击continue
    private func installAddressContinue() {
        if self.checkIfChildVCExist(installAddressVC) {
            self.installAddressVC.getData()
        }
        if self.installAddressVC.editFlag == true {//编辑
            self.installAddressVC.editFlag = false
            self.installAddressVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.installAddressVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: installAddressVC, toVC: siteAttributesVC)
        }
    }
    //MARK: -  点击continue
    private func siteAttributesContinue() {
        if self.siteAttributesVC.editFlag == true {//编辑
            self.siteAttributesVC.editFlag = false
            self.siteAttributesVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.siteAttributesVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: siteAttributesVC, toVC: ownerInfomationVC)
        }
    }
    //MARK: -  点击continue
    private func ownerInfomationContinue() {
        if self.ownerInfomationVC.editFlag == true {//编辑
            self.ownerInfomationVC.editFlag = false
            self.ownerInfomationVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.ownerInfomationVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: ownerInfomationVC, toVC: scheculeDateOfInstallVC)
        }
    }
    //MARK: -  点击continue
    private func scheculeDateOfInstallContinue() {
        if self.checkIfChildVCExist(scheculeDateOfInstallVC) {
            self.scheculeDateOfInstallVC.getData()
        }
        //在Review中点击Edit编辑时，才校验
        if self.scheculeDateOfInstallVC.customView.createJobReviewEditFlag {
            CreateJobModelHelper.shared.checkIfSystemInfoValid(loadingFlag: false)
        }
        if self.scheculeDateOfInstallVC.editFlag == true {//编辑
            self.scheculeDateOfInstallVC.editFlag = false
            self.scheculeDateOfInstallVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.scheculeDateOfInstallVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: scheculeDateOfInstallVC, toVC: systemDetailVC)
        }
    }
    //MARK: -  点击continue
    private func systemDetailContinue() {
        if self.checkIfChildVCExist(systemDetailVC) {
            self.systemDetailVC.getData()
        }
        //在Review中点击Edit编辑时，才校验
        if self.systemDetailVC.customView.createJobReviewEditFlag {
            CreateJobModelHelper.shared.checkIfSystemInfoValid(loadingFlag: false)
        }
        if self.systemDetailVC.editFlag == true {//编辑
            self.systemDetailVC.editFlag = false
            self.systemDetailVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.systemDetailVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: systemDetailVC, toVC: installerDetailVC)
        }
    }
    //MARK: -  点击continue
    private func installerDetailContinue() {
        //在Review中点击Edit编辑时，才校验
        if self.installerDetailVC.customView.createJobReviewEditFlag {
            CreateJobModelHelper.shared.checkIfInstallDetailValid(loadingFlag: false)
        }
        if self.installerDetailVC.editFlag == true {//编辑
            self.installerDetailVC.editFlag = false
            self.installerDetailVC.customView.createJobReviewEditFlag = false
            self.toNextVC(fromVC: self.installerDetailVC, toVC: self.reviewDetailVC)
        }else{//新建
            self.toNextVC(fromVC: installerDetailVC, toVC: reviewDetailVC)
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
}
